<?php

/**
 * Erra_Fields_Base_Object - High level standard object
 *
 * @package
 * @author Created by Alexis Eric
 * @copyright Copyright (c) 2012 - Erra Prod, http://www.erraprod.com
 * @version $1.05$
 * @access public
 */
class Erra_Fields_Base_Object extends Erra_Fields_Base_Field {

    protected $_buildArgs = null;
    protected $_submitOnChange = false;
    protected $fields = array();
    protected $fieldObjects = null;
    protected $_renderEngine = null;
    protected $_readOnly = false;
    protected $_fetchFromList = false;
    protected $_addText = 'Cliquez ici pour ajouter un élément';
    protected $_attachedActionMethod = null;
    protected $_postListInit = false;
    protected $_orderBy = null;
    protected $_isAjax = true;
    protected $crud = 'CRUD';

    public function __construct($objectId, $buildArgs = null) {
        parent::__construct($objectId);
        $this->setObjectId($objectId);
        $this->setBuildArgs($buildArgs);
        $pageSize = Erra_Fields_Search_PageSize::create('search__paginator__pageSize');
        $PageNumber = Erra_Fields_Search_PageNumber::create('search__paginator__pageNumber');
        $this->addFields($pageSize, $PageNumber);
    }

    public function _form($actionMethod = 'AdminAction/saveSql') {
        if (method_exists($this, '__form')) {
            return Erra_Core_Lexer::messageRules($this->__form($actionMethod));
        }
        return Erra_System_FormRenderer::editFieldAjax($this, $actionMethod);
    }

    public function _editInPlace($actionMethod, $htmlCode = null, $displayParameters = null) {

        $fieldHtml = is_null($htmlCode) ? $this->_render($displayParamters) : $htmlCode;
        if (is_null($fieldHtml)) {
            $res .= Erra_Utils_Tools::message('warning', __("Can't find render method `_render` or `_render_list` for object `") . $this->getObjectClass() . '`');
        }
        $editInPlaceClass = '';
        $editInPlaceClass = (config_get('application.forms_theme') == 'native') ? 'erra-form-native-edit_in_place' : $editInPlaceClass;
        $editInPlaceClass = (config_get('application.forms_theme') == 'jqueryui') ? 'ui-edit_in_place' : $editInPlaceClass;

        $res = Erra_System_FormRenderer::startEditInPlaceDiv($this, $actionMethod, $editInPlaceClass);
        $res .= $fieldHtml;
        $res .= Erra_System_FormRenderer::endEditInPlaceDiv();
        return $res;
    }

    public function _render($displayArgs = null) {
        $res = '';
        if (method_exists($this, '__render')) {
            $res .= Erra_Core_Lexer::messageRules($this->__render($displayArgs));
        } else {
            $res .= Erra_Utils_Tools::message('warning', get_class($this) . '->__render() ' . __('not implemented'));
        }
        $res = (!strlen($res)) ? Erra_Utils_Tools::message('warning', 'the method __render() for field `' . $this->getObjectId() . '`, class `' . get_class($this) . '`  displays nothing') : $res;
        if (Erra_System_AccessControl::is('admin') || Erra_System_AccessControl::is('dev')) {
            $res = '<span id="erra-admin-hover-' . $this->getobjectId() . '-' . $this->getTableName() . '">' . $res . '</span>';
        }
        return $res;
    }

    public function _render_list($displayArgs = null) {
        $res = '';
        if (method_exists($this, '__render_list')) {
            $res .= Erra_Core_Lexer::messageRules($this->__render_list($displayArgs));
        } else {
            $res .= Erra_Utils_Tools::message('warning', get_class($this) . '->__render_list() ' . __('not implemented'));
        }
        $res = (!strlen($res)) ? Erra_Utils_Tools::message('warning', 'the method __render_list() for field `' . $this->getObjectId() . '`, class `' . get_class($this) . '` returns nothing') : $res;
        if (Erra_System_AccessControl::is('admin') || Erra_System_AccessControl::is('dev')) {
            $res = '<span id="erra-admin-hover-' . $this->getobjectId() . '-' . $this->getTableName() . '">' . $res . '</span>';
        }
        return $res;
    }

    public function makeEditable() {
        // FIXME : add a system to retreive field from factory
        Erra_System_Factory::$adminFields[$this->getObjectClass() . '.' . $this->getObjectId() . '.' . $this->getTableName()] = $this;
        return $this;
    }

    public static function getObject($object_id, $flags = null, $WHERE_CLAUSE = null, $buildArgs = null) {
        return Erra_System_Factory::getFieldObject(get_called_class(), $object_id, $flags, $WHERE_CLAUSE, $buildArgs);
    }

    public static function getList($WHERE_CLAUSE = null, $buildArgs = null, $ALL = null) {
        return Erra_System_Factory::getFieldList(get_called_class(), $WHERE_CLAUSE, $buildArgs, $ALL);
    }

    public function getObjectArray($WHERE_CLAUSE = null) {
        if ($this->fieldObjects == null) {
            $list = array();
            // TODO : secure where clause
            $result = Erra_Core_DatabasePdo::_SELECT('SELECT * FROM ' . $this->getTableName() . ' ' . $WHERE_CLAUSE);
            if (Erra_Core_DatabasePdo::rowCount()) {
                $fieldClass = get_class($this);
                while ($row = $result->fetch()) {
                    $object = new $fieldClass($row['object_id']);
                    $object->setDescription($this->getDescription());
                    // TODO : clone more properties ?
                    $object->setRawData(Erra_Serialization_SerializerJson::unserializeField($object, $row));
                    $list[] = $object;
                }
            }
            $this->fieldObjects = $list;
        }
        return $this->fieldObjects;
    }

    public function getObjectDataArray(
    $key = null) {
        if (is_numeric($key) && isset($this->fields[$key])) {
            return $this->fields[$key];
        }
        return $this->fields;
    }

    public function setObjectDataArray(
    $array_of_data) {
        $this->fields = $array_of_data;
        return $this;
    }

    /** Getters and setters */
    public function getBuildArgs() {
        return

                $this->_buildArgs;
    }

    public function setBuildArgs(
    $buildArgs) {
        $this->_buildArgs = $buildArgs;
        return $this;
    }

    public function getSubmitOnChange() {
        return

                $this->_submitOnChange;
    }

    public function setSubmitOnChange(
    $submitOnChange) {
        $this->_submitOnChange = $submitOnChange;
        return $this;
    }

    public function getReadOnly() {
        return

                $this->_readOnly;
    }

    public function setReadOnly(
    $readOnly) {
        $this->_readOnly = $readOnly;
        return $this;
    }

    public function getRenderEngine() {
        return

                $this->_renderEngine;
    }

    public function setRenderEngine(
    $renderEngine) {
        if (strlen($renderEngine)) {
            $this->_renderEngine = $renderEngine;
        }
        return $this;
    }

    public function isCrud($acroynm) {
        return stristr($this->crud, $acroynm) ? true : false;
    }

    public function getCrud() {
        return

                $this->crud;
    }

    public function setCrud(
    $crud) {
        $this->crud = $crud;
        return $this;
    }

    public function isFetchFromList() {
        return $this->_fetchFromList;
    }

    public function setFetchFromList($fetchFromList) {
        $this->_fetchFromList = $fetchFromList;
    }

    public function getAddText() {
        return $this->_addText;
    }

    public function setAddText($addText) {
        $this->_addText = $addText;
        return $this;
    }

    public function getAttachedActionMethod() {
        return $this->_attachedActionMethod;
    }

    public function setAttachedActionMethod($_AttachedActionMethod) {
        $this->_attachedActionMethod = $_AttachedActionMethod;
        return $this;
    }

    public function getFieldObjects() {
        return $this->fieldObjects;
    }

    public function setOrderBy($orderBy) {
        $this->_orderBy = $orderBy;
    }

    public function getOrderBy() {
        return $this->_orderBy;
    }

    public function setFieldObjects($fieldObjects) {
        $this->fieldObjects = $fieldObjects;
    }

    public function postInit() {
        
    }

    public function isAjax() {
        return $this->_isAjax;
    }

    public function setAjax($isAjax) {
        $this->_isAjax = $isAjax;
        return $this;
    }

    public function _postListInit() {
        if ($this->_postListInit == false) {
            $this->postListInit();
            $this->_postListInit = true;
        }
    }

    public function postListInit() {
        
    }

}

?>